package com.lee.controller.system;

import java.util.List;
import java.util.Map;

import com.lee.utils.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.dubbo.config.annotation.Reference;
import com.lee.aspect.Log;
import com.lee.base.BaseController;
import com.lee.base.Result;
import com.lee.constant.BusinessType;
import com.lee.page.TableSupport;
import com.lee.shiro.service.PasswordService;
import com.lee.system.api.IPostService;
import com.lee.system.api.IRoleService;
import com.lee.system.api.IUserService;
import com.lee.system.entity.Post;
import com.lee.system.entity.Role;
import com.lee.system.entity.User;
import com.lee.utils.ExcelUtil;
import com.lee.utils.StringUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 用户 信息操作处理
 * 
 * @author LiPengFei
 *
 */
@Api("用户信息操作")
@Controller
@RequestMapping("/system/user")
public class UserController extends BaseController {
	private String prefix = "system/user";

	@Reference
	private IUserService userService;
	@Reference
	private IPostService postService;
	@Reference
	private IRoleService roleService;
	@Autowired
	PasswordService passwordService;

	@GetMapping()
	public String user() {
		return prefix + "/user";
	}

	/**
	 * 查询用户列表
	 */
	@ApiOperation("获取用户列表")
	@ResponseBody
	@PostMapping("/list")
	public Result list(User user) {
		return Result.ok().page(userService.selectUserList(user, TableSupport.getPageDomain()));
	}

	/**
	 * 新增用户
	 * 
	 * @param map
	 * @return
	 */
	@GetMapping("/add")
	@Log(title = "用户管理", action = BusinessType.INSERT)
	public String add(Map<String, Object> map) {
		map.put("posts", postService.selectPostAll());
		map.put("roles", roleService.selectRoleAll());
		return prefix + "/add";
	}

	/**
	 * 删除用户
	 * 
	 * @param userIds
	 * @return
	 */
	@ApiOperation("删除用户")
	@PostMapping("/remove")
	@ResponseBody
	@Log(title = "用户管理", action = BusinessType.DELETE)
	public Result reomve(String ids) {
		try {
			userService.deleteUserByIds(ids);
			return Result.ok("操作成功");
		} catch (Exception e) {
			return Result.error(e.getMessage());
		}
	}

	/**
	 * 保存用户
	 * 
	 * @param user
	 * @return
	 */
	@ApiOperation("保存用户")
	@PostMapping("/save")
	@Log(title = "用户管理", action = BusinessType.SAVE)
	@ResponseBody
	public Result SavedRequest(User user) {
		if (StringUtils.isNotNull(user.getUserId()) && user.getUserId() == 1L) {
			return Result.error("不能修改超级用户");
		}
		user.setUpdateBy(ShiroUtils.getLoginName());
		user.saltRandom();
		user.setPassword(passwordService.encryptPassword(user.getLoginName(), user.getPassword(), user.getSalt()));
		return userService.saveUser(user) > 0 ? Result.ok() : Result.error();
	}

	@ApiOperation("重置用户密码")
	@Log(title = "用户管理", action = BusinessType.OTHER)
	@ResponseBody
	@GetMapping("/restPassWord/{userId}")
	public Result restPassWord(@PathVariable("userId") Long userId) {
		User user = userService.selectUserById(userId);
		user.saltRandom();
		user.setPassword(passwordService.encryptPassword(user.getLoginName(), "123456", user.getSalt()));
		if (userService.restPassWord(user) > 0) {
			return Result.ok();
		}
		return Result.error();

	}

	/**
	 * 修改用户
	 * 
	 * @param userId
	 * @param map
	 * @return
	 */
	@GetMapping("/edit/{userId}")
	@Log(title = "用户管理", action = BusinessType.UPDATE)
	public String edit(@PathVariable("userId") Long userId, Map<String, Object> map) {
		User user = userService.selectUserById(userId);
		List<Role> roles = roleService.selectRolesByUserId(userId);
		List<Post> posts = postService.selectPostsByUserId(userId);
		map.put("user", user);
		map.put("roles", roles);
		map.put("posts", posts);
		return prefix + "/edit";
	}

	/**
	 * 校验用户名唯一
	 */
	@ApiOperation("校验用户名唯一")
	@PostMapping("/checkLoginNameUnique")
	@ResponseBody
	public Result checkLoginNameUnique(String loginName) {
		if (userService.checkLoginNameUnique(loginName) == 0) {
			return Result.ok();
		}
		return Result.error();
	}

	/**
	 * 校验邮箱唯一
	 * 
	 * @param user
	 * @return
	 */
	@ApiOperation("校验邮箱唯一")
	@PostMapping("/checkEmailUnique")
	@ResponseBody
	public Result checkEmailUnique(User user) {
		if (userService.checkEmailUnique(user)) {
			return Result.ok();
		}
		return Result.error();
	}

	/**
	 * 校验电话唯一
	 * 
	 * @param user
	 * @return
	 */
	@ApiOperation("校验电话唯一")
	@PostMapping("/checkPhoneUnique")
	@ResponseBody
	public Result checkPhoneUnique(User user) {
		if (userService.checkPhoneUnique(user)) {
			return Result.ok();
		}
		return Result.error();
	}

	/**
	 * 导出用户
	 * 
	 * @param user
	 * @return
	 * @throws Exception
	 */
	@ApiOperation("用户导出")
	@Log(title = "用户管理", action = BusinessType.EXPORT)
	@PostMapping("/export")
	@ResponseBody
	public Result export(User user) throws Exception {
		try {
			List<User> list = userService.selectUserList(user);
			ExcelUtil<User> util = new ExcelUtil<User>(User.class);
			return util.exportExcel(list, "user");
		} catch (Exception e) {
			return Result.error("导出Excel失败，请联系网站管理员！");
		}
	}

}
